package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpSession;
import org.example.mapper.UsersMapper;
import org.example.pojo.Result;
import org.example.pojo.Users;
import org.example.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("auth")
//@CrossOrigin(origins = "*")  // 允许所有来源
public class AutoController 
{
	@Autowired
	UsersService usersService;
	
	@GetMapping("test")
	public Result test()
	{
		return Result.ok();
	}
	
	@GetMapping("login")
	public Result login(HttpSession httpSession,@RequestParam String username,@RequestParam String password)
	{
		QueryWrapper qw = new QueryWrapper();
		qw.eq("username",username);
		qw.eq("password",password);
		Users one = usersService.getOne(qw);
		if(one == null)
		{
			return Result.error("登录失败");
		}
		httpSession.setAttribute("uid",one.getId());
		return Result.ok("登录成功");
	}
}
